<template>
    <view>
        <view class="cu-modal bottom-modal" :class="modalName=='bottomModal'?'show':''">
		<view class="cu-dialog">
			
			<!--padding-xl-->
			<view class="padding bg-white">
				<form>
					<!-- -->
					
					<view class="cu-form-group padding-sm">
						<js-uploadfile  v-model="fpData.dataMap"  :biz-key="bizKey" :biz-type="bizType"></js-uploadfile>
					</view>
					<view class="cu-form-group ">
						<view class="title">日期选择</view>
						<picker mode="date" :value="date" :start="startDate" @change="DateChange">
							<view class="picker">
								{{date }}
							</view>
						</picker>
					</view>
					

				</form>
			</view>
			<view class="cu-bar bg-white flex padding-left padding-right padding-bottom">
				<!-- <view class="action text-green" @tap="confirm">确定</view>
				<view class="action text-blue" @tap="hideModal">取消</view> -->
				<button class="cu-btn round  bg-confirm lg" @tap="confirm">确定</button>
				<button class="cu-btn round  lines-red lg margin-left" @tap="hideModal">取消</button>
			</view>
		</view>
	</view>
    </view>
</template>
<script>
    export default {
		props: {
			modalName:{
				type: String,
				default: 'bottomModal'
			},
			bizType:{
				type: String,
				default: ''
			},
			bizKey:{
				type: String,
				default: ''
			},
			status:{
				type: Number,
				default: ''
			},
        },
        data() {
            return {
                // modalName:'bottomModal',
				imgList:[],
				date:new Date().toISOString().substring(0, 10),
				uploadOptions:{},
				fpData:{},
				delayDay:0,
				startDate: '2015-09-01', // picker的开始日期
            }
        },
		created(){
			console.log(this.bizType,'bizType',this.bizKey);
			this.initDateConfig();
		},
		watch: {
			// 监听status变化
			status: {
				handler(newVal, oldVal) {
					console.log('status changed:', newVal, oldVal);
					this.initDateConfig();
				},
				immediate: true // 立即执行一次
			},
			// 监听modalName变化，当弹窗显示时重新初始化
			modalName: {
				handler(newVal, oldVal) {
					console.log('modalName changed:', newVal, oldVal);
					if (newVal === 'bottomModal') {
						this.initDateConfig();
					}
				}
			}
		},
        methods: {
			// 初始化日期配置
			initDateConfig() {
				console.log('initDateConfig called, status:', this.status);
				if (this.status === 4) {
					console.log('status is 3, requesting config...');
					// 当status为3时，请求配置获取延迟天数
					this.$u.api.zfgs.getConfigValue({
						configKey: 'zf:withdrawal:days:qr'
					}).then((res) => {
						console.log('getConfigValue response:', res);
						this.delayDay = parseInt(res) || 0;
						console.log('delayDay set to:', this.delayDay);
						this.setStartDate();
					}).catch((error) => {
						console.error('获取配置失败:', error);
						this.delayDay = 0;
						this.setStartDate();
					});
				} else {
					console.log('status is not 3, using default config');
					// 其他状态使用默认配置
					this.setStartDate();
				}
			},

			// 设置picker的开始日期
			setStartDate() {
				if (this.status === 4 && this.delayDay > 0) {
					// 今天开始加上delayDay天
					const today = new Date();
					today.setDate(today.getDate() - this.delayDay);
					this.startDate = this.formatDate(today);
					console.log('calculated startDate:', this.startDate);

					// 如果当前选择的日期早于开始日期，则更新为开始日期
					const currentDate = new Date(this.date);
					const minDate = new Date(this.startDate);
					if (currentDate < minDate) {
						this.date = this.startDate;
						console.log('updated date to:', this.date);
					}
				} else {
					// 默认开始日期
					this.startDate = '2015-09-01';
					console.log('using default startDate:', this.startDate);
				}
			},

			// 格式化日期为YYYY-MM-DD格式
			formatDate(date) {
				const year = date.getFullYear();
				const month = String(date.getMonth() + 1).padStart(2, '0');
				const day = String(date.getDate()).padStart(2, '0');
				return `${year}-${month}-${day}`;
			},

			imgListChange(e){
				console.log(e);
			},
            showModal() {
                // this.modalName = 'bottomModal'
				this.$emit('modalNameEmit', 'bottomModal')
            },
            hideModal() {
                // this.modalName = null
				this.$emit('modalNameEmit', null)
            },
			DateChange(e) {
				this.date = e.detail.value
			},
			confirm(){
				if(!this.fpData.dataMap[this.bizType] ){
					this.$u.toast("图片必传");
					return;
				}
				this.$u.toast("确认成功");
				// this.modalName = null
				// 把fdData传递给父组件
				this.$emit("fdData",{fdData:this.fpData,approachdate:this.date});
				this.$emit("modalNameEmit",null);
			},
		}
    }

</script>
<style scoped lang="scss">
.cu-modal{
	z-index: 998;
}
.bg-confirm {
  background: #3e97b0;
  color: #ffffff;
}
.cu-btn{
	flex: 1;
}
</style>
